/**
 * Copyright (c) 2007, Blue Hole Software. All rights reserved.
 * Code licensed under the Apache 2.0 License:
 * http://www.apache.org/licenses/
 */
package org.bhf.providers.security.authorization.dbpolicy;

import org.bhf.security.common.Role;

import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Query;
import javax.persistence.Table;
import java.security.Permission;
import java.security.Principal;
import java.util.List;

/**
 * One Permission grants certain permissions in the context of a single project. Permissions may be grante to Roles
 * or Users.
 * @exclude
 */
@Entity
@NamedQueries({
    @NamedQuery(
    name  = "PermissionEntity.permissionsForPrincipal",
    query = "SELECT p FROM PermissionEntity p WHERE " +
        "p.principalType = :principalType and p.principalName = :principalName and p.type = :type"
    )
})
@Table( name = "POLICY" )
public class PermissionEntity
{
    private long            id;
    private PrincipalType   principalType;
    private String          principalName;
    private String          type;
    private String          name;
    private String          actions;

    @Id
    @GeneratedValue
    public long             getId()
    {
        return id;
    }

    public void             setId( final long id )
    {
        this.id = id;
    }

    @Enumerated( EnumType.STRING )
    public PrincipalType    getPrincipalType()
    {
        return principalType;
    }

    public void             setPrincipalType( final PrincipalType principalType )
    {
        this.principalType = principalType;
    }

    public String           getPrincipalName()
    {
        return principalName;
    }

    public void             setPrincipalName( final String principalName )
    {
        this.principalName = principalName;
    }

    public String           getType()
    {
        return type;
    }

    public void             setType( final String type )
    {
        this.type = type;
    }

    public String           getName()
    {
        return name;
    }

    public void             setName( final String name )
    {
        this.name = name;
    }

    public String           getActions()
    {
        return actions;
    }

    public void             setActions( final String actions )
    {
        this.actions = actions;
    }

    //
    // Queries
    //

    @SuppressWarnings( "unchecked" )
    public static List<PermissionEntity>    byPrincipalAndPermission( final EntityManager entityManager , final Principal principal, final Permission permission )
    {
        final Query query =entityManager.createNamedQuery( "PermissionEntity.permissionsForPrincipal" );

        query.setParameter( "principalType", principal instanceof Role ? PrincipalType.ROLE : PrincipalType.USER );
        query.setParameter( "principalName", principal.getName() );
        query.setParameter( "type", permission.getClass().getName() );

        return query.getResultList();
    }
}
